package main

import (
	"fmt"
	"io"
	"net/http"
	"os"
	"strings"
)

type httpHandler func(http.ResponseWriter, *http.Request)

func main() {
	http.HandleFunc("/healthz", healthz)
	http.ListenAndServe(":80", nil)
}

func (f httpHandler) setHead(writer http.ResponseWriter, request *http.Request) {
	header := request.Header
	responseHeader := writer.Header()

	for k, v := range header {
		value := strings.Join(v, ";")
		responseHeader.Add(k, value)
	}
}

func (f httpHandler) setVersion(writer http.ResponseWriter, request *http.Request) {
	version := os.Getenv("VERSION")
	header := writer.Header()
	header.Set("VERSION", version)
}

// TODO 获取status未成功
func (f httpHandler) log(writer http.ResponseWriter, request *http.Request) {
	host := request.Host
	fmt.Printf("ip: %s", host)
}

func healthz(writer http.ResponseWriter, request *http.Request) {
	myhandler.setHead(writer, request)
	myhandler.setVersion(writer, request)
	myhandler(writer, request)
	myhandler.log(writer, request)
}

var myhandler httpHandler = func(writer http.ResponseWriter, request *http.Request) {
	io.WriteString(writer, "ok")
}
